package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.pojo.User;
import com.cskaoyan.bean.pojo.UserExample;
import com.cskaoyan.bean.wx.auth.bo.RegisterBo;
import com.cskaoyan.bean.wx.auth.bo.ResetBo;
import com.cskaoyan.bean.wx.auth.vo.UserInfoVo;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.wx.WxAuthService;
import com.cskaoyan.util.Md5utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Name : WxAuthServiceImpl.java
 * @Time : 2021/8/15 14:48
 * @Author : creamycandy
 * @Software : IntelliJ IDEA
 */
@Service
@Transactional
public class WxAuthServiceImpl implements WxAuthService {

    @Autowired
    UserMapper userMapper;


    @Override
    public UserInfoVo getUserInfo(String username) {
        User user = userMapper.selectByUsername(username);
        UserInfoVo userInfoVo = new UserInfoVo();
        userInfoVo.setNickName(username);
        if(user.getAvatar()!=null){
            userInfoVo.setAvatarUrl(user.getAvatar());
        }
        return userInfoVo;
    }

    @Override
    public Integer register(RegisterBo registerBo) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<User> users = userMapper.selectByExample(userExample);
        for (User user : users) {
            if (user.getMobile().equals(registerBo.getMobile())) {
                return 0;
            }
            if (user.getUsername().equals(registerBo.getUsername())) {
                return 1;
            }
        }
        User user = new User();
        user.setUsername(registerBo.getUsername());
        String password = Md5utils.getMd5Result(registerBo.getPassword());
        user.setPassword(password);
        user.setMobile(registerBo.getMobile());
        user.setLastLoginIp("");
        user.setAddTime(new Date(System.currentTimeMillis()));
        user.setUpdateTime(user.getAddTime());
        user.setDeleted(false);
        userMapper.insertSelective(user);
        return 2;
    }

    @Override
    public Integer reset(ResetBo resetBo) {
        User user = userMapper.selectByMobile(resetBo.getMobile());
        if (user == null) {
            return 0;
        }
        String password = Md5utils.getMd5Result(resetBo.getPassword());
        user.setPassword(password);
        user.setUpdateTime(new Date(System.currentTimeMillis()));
        userMapper.updateByPrimaryKeySelective(user);
        return 1;
    }

    @Override
    public void loginWrong(String username) {
        User user = userMapper.selectByUsername(username);
        if(user.getWrongNum()==null){
            user.setWrongNum(0);
        }
        user.setWrongNum(user.getWrongNum() + 1);
        if (user.getWrongNum() > 3) {
            user.setWrongTime(new Date(System.currentTimeMillis()));
        }
        userMapper.updateById(user.getId(),user.getWrongNum(),user.getWrongTime());
    }

    @Override
    public Boolean login(String username) {
        User user = userMapper.selectByUsername(username);
        Date wrongTime = user.getWrongTime();
        if(wrongTime == null){
            return true;
        }
        long time = wrongTime.getTime();
        if (System.currentTimeMillis() - time > 30000) {
            return true;
        }
        return false;
    }

    @Override
    public void loginSuccess(String username) {
        User user = userMapper.selectByUsername(username);
        user.setWrongNum(0);
        userMapper.updateById(user.getId(),user.getWrongNum(),user.getWrongTime());
    }
}
